package com.sugar.queryinfo.module.business.dal.mysql.userbalancerecord;

import com.sugar.queryinfo.framework.common.pojo.PageResult;
import com.sugar.queryinfo.framework.mybatis.core.mapper.BaseMapperX;
import com.sugar.queryinfo.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.sugar.queryinfo.module.business.controller.admin.config.vo.HomeStatisticsRespVO;
import com.sugar.queryinfo.module.business.dal.dataobject.userbalancerecord.UserBalanceRecordDO;
import com.sugar.queryinfo.module.business.dal.dataobject.userextend.UserExtendDO;
import com.sugar.queryinfo.module.system.dal.dataobject.dept.DeptDO;
import com.sugar.queryinfo.module.system.dal.dataobject.user.AdminUserDO;
import org.apache.ibatis.annotations.Mapper;
import com.sugar.queryinfo.module.business.controller.admin.userbalancerecord.vo.*;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 余额变动记录 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface UserBalanceRecordMapper extends BaseMapperX<UserBalanceRecordDO> {

    default PageResult<UserBalanceRecordRespVO> selectPage(UserBalanceRecordPageReqVO reqVO) {
        return selectJoinPage(reqVO, UserBalanceRecordRespVO.class, new MPJLambdaWrapperX<UserBalanceRecordDO>()
                .selectAll(UserBalanceRecordDO.class)
                .selectAs(AdminUserDO::getNickname, UserBalanceRecordRespVO::getNickname)
                .selectAs(AdminUserDO::getMobile, UserBalanceRecordRespVO::getMobile)
                .selectAs(AdminUserDO::getUsername, UserBalanceRecordRespVO::getUsername)
                .selectAs(DeptDO::getName, UserBalanceRecordRespVO::getDeptName)
                .leftJoin(AdminUserDO.class, AdminUserDO::getId, UserExtendDO::getUserId)
                .leftJoin(DeptDO.class, DeptDO::getId, AdminUserDO::getDeptId)
                .likeIfPresent(AdminUserDO::getUsername, reqVO.getUserName())
                .likeIfPresent(DeptDO::getName, reqVO.getDeptName())
                .eqIfPresent(UserBalanceRecordDO::getTitle, reqVO.getTitle())
                .eqIfPresent(UserBalanceRecordDO::getType, reqVO.getType())
                .betweenIfPresent(UserBalanceRecordDO::getRecordTime, reqVO.getRecordTime())
                .betweenIfPresent(UserBalanceRecordDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(UserBalanceRecordDO::getId));
    }

    @Select("select count(1) from system_login_log sll where sll.deleted = 0 and sll.`result` = 0 and sll.log_type = 100 and sll.create_time between #{param1} and #{param2}")
    Long selectLoginCount(LocalDateTime startTime, LocalDateTime endTime);

    @Select("select count(1) from system_login_log sll where sll.deleted = 0 and sll.`result` = 0 and sll.log_type = 100")
    Long selectLoginTotalCount();

    @Select("select DATE_FORMAT(bubr.record_time, '%Y-%m-%d') AS date, count(1) as `count` from business_user_balance_record bubr " +
            "where bubr.deleted = 0 and `type` = 0 and bubr.record_time >= #{param1} group by date")
    List<HomeStatisticsRespVO.Statistics> selectSqStatistics(LocalDateTime localDateTime);

    @Select("select DATE_FORMAT(sll.create_time, '%Y-%m-%d') AS date, count(1) as `count` from system_login_log sll " +
            "where sll.deleted = 0 and sll.`result` = 0 and sll.log_type = 100 and sll.create_time >= #{param1} group by date")
    List<HomeStatisticsRespVO.Statistics> selectLoginStatistics(LocalDateTime localDateTime);
}